package com.appkubes.fintech.admin.application.risk.rule.controller;

import com.appkubes.fintech.admin.application.risk.rule.entity.vo.MallBaiRongVO;
import com.appkubes.fintech.admin.application.risk.rule.entity.vo.RiskRuleDelVO;
import com.appkubes.fintech.admin.application.risk.rule.entity.vo.RiskRuleVO;
import com.appkubes.fintech.admin.application.risk.rule.service.RiskRuleService;
import com.appkubes.fintech.admin.application.risk.rule.service.RiskService;
import com.appkubes.fintech.admin.task.job.BaiRongTiming;
import com.appkubes.fintech.common.request.PageVO;
import com.appkubes.fintech.common.response.Response;
import com.appkubes.fintech.core.po.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @Author: leeming
 * @Date: 2020-02-04 16:16
 * @Description:
 */
@Slf4j
@Api(tags = "风控配置")
@RestController
@RequestMapping("/risk/")
public class RuleController {

    @Resource
    RiskRuleService riskRuleService;
    @Autowired
    RiskService riskService;

    @ApiOperation("获取分类列表")
    @GetMapping("/getTypeList")
    public Response getTypeList() {
        List<FintechRiskRule> lists = riskRuleService.getRiskRuleDao().selectByExample(null);

        return Response.success(lists);
    }

    @ApiOperation("根据分类信息获取数据列表")
    @PostMapping("/getList")
    public Response<PageInfo<FintechDataBlackList>> get(@Valid PageVO pageVO,String riskType, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.fail("参数检查失败");
        }
        FintechRiskRuleExample example = new FintechRiskRuleExample();
        example.createCriteria().andRiskTypeEqualTo(riskType);
        PageHelper.startPage(pageVO.getPage(), pageVO.getPageSize());
        List<FintechRiskRule> lists = riskRuleService.getRiskRuleDao().selectByExample(example);
        PageInfo pageInfo = new PageInfo(lists);

        return Response.success(pageInfo);
    }

    @ApiOperation("添加规则")
    @PostMapping("/add")
    public Response add(@RequestBody @Valid RiskRuleVO vo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.fail("参数检查失败");
        }
        FintechRiskRuleExample example = new FintechRiskRuleExample();
        example.createCriteria().andRiskTypeEqualTo(vo.getRiskType()).andRuleCodeEqualTo(vo.getRuleCode());
        List<FintechRiskRule> lists = riskRuleService.getRiskRuleDao().selectByExample(example);
        if (lists != null && lists.size()>0) {
            return Response.fail("已经存在");
        }
        FintechRiskRule rule = new FintechRiskRule();
        BeanUtils.copyProperties(vo, rule);
        rule.setCreateTime(new Date());
        int result = riskRuleService.getRiskRuleDao().insert(rule);
        if (result==1) {
            return Response.success();
        }
        return Response.fail("增加失败");
    }

    @ApiOperation("删除规则")
    @PostMapping("/del")
    public Response del(@RequestBody @Valid RiskRuleDelVO vo, BindingResult bindingResult) {
        FintechRiskRuleKey key = new FintechRiskRuleKey();
        BeanUtils.copyProperties(vo, key);
        int result = riskRuleService.getRiskRuleDao().deleteByPrimaryKey(key);
        if (result==1) {
            return Response.success();
        }
        return Response.fail("删除失败");
    }

    @ApiOperation("修改规则")
    @PostMapping("/update")
    public Response update(@RequestBody @Valid RiskRuleVO vo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.fail("参数检查失败");
        }
        FintechRiskRuleKey key = new FintechRiskRuleKey();
        BeanUtils.copyProperties(vo, key);

        FintechRiskRule rule = riskRuleService.getRiskRuleDao().selectByPrimaryKey(key);
        if (rule == null) {
            return Response.fail("记录不存在");
        }
        BeanUtils.copyProperties(vo, rule);
        int result = riskRuleService.getRiskRuleDao().updateByPrimaryKey(rule);
        if (result==1) {
            return Response.success();
        }
        return Response.fail("修改失败");
    }
    /**
     * 获取token和swit_number访问网页
     * @return
     */
    @ApiOperation("百融访问网页")
    @GetMapping("/StrategyDemo")
    public void StrategyDemo(MallBaiRongVO mallBaiRongVO,
                             HttpServletResponse resp) throws IOException {
        String str = riskService.StrategyDemoAppkey2(mallBaiRongVO);
        mallBaiRongVO.setSwitNumber(str);
        riskService.InsertBaiRong(mallBaiRongVO);
        resp.sendRedirect("https://apiservice.100credit.com/v/#/userReport/"+str+"/"+ BaiRongTiming.st);

    }
}
